/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.DataSetDAO;

import com.nc.kyiv.yellow.sociallibrary.BookInfo.BookAuthors;
import com.nc.kyiv.yellow.sociallibrary.BookInfo.BookDetailedRating;
import com.nc.kyiv.yellow.sociallibrary.BookInfo.BookGenres;
import com.nc.kyiv.yellow.sociallibrary.BookInfo.CurrentUserScore;
import com.nc.kyiv.yellow.sociallibrary.ConnectionManager.ConnectionManager;
import com.nc.kyiv.yellow.sociallibrary.POJO.Book;
import com.nc.kyiv.yellow.sociallibrary.POJO.BookAuthor;
import com.nc.kyiv.yellow.sociallibrary.POJO.BookRating;
import com.nc.kyiv.yellow.sociallibrary.constants.DataBaseConstants;
import com.nc.kyiv.yellow.sociallibrary.helper.DashboardMapFilters;
import com.nc.kyiv.yellow.sociallibrary.similarUse.AbstractDataSet;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import oracle.jdbc.OracleTypes;

/**
 *
 * @author Alena
 */
public class DashboardDataSet extends AbstractDataSet{

    public DashboardDataSet() throws Exception {
            super();
    }
    
    @Override
    public List<Book> getDataSet(Map<String, String> params) throws SQLException, Exception {
       
        List<BookAuthor> authors = new ArrayList<>();
        List<String> genres = new ArrayList<>();
        List<BookRating> rating = new ArrayList<>();
        List<Book> dashboard = new ArrayList<>();
        BookAuthors ba = new BookAuthors();
        BookGenres bg = new BookGenres();
        BookDetailedRating bdr = new BookDetailedRating();
        CurrentUserScore cus = new CurrentUserScore();
        int curUserScore = 0;
        ResultSet rs = null;
        CallableStatement cStmt = null;

        Connection conn = null;
                conn = ConnectionManager.getConnection();
        
                 try {
                           
            DashboardMapFilters filtersMap = new DashboardMapFilters();
            Map<String, String> ordersFilterMap = filtersMap.getOrders();
            
            int genreFilter = 0;
            if (!params.get("filterTwoValue").equals("all genres")) {
                Map constant = DataBaseConstants.getGenre();
                genreFilter = (int) constant.get(params.get("filterTwoValue"));
            }
            String orderFilter = params.get("filterOneValue");
            
            int publishedBy = (int)Integer.parseInt(params.get("publishedBy"));
            int pageNumber = (int)Integer.parseInt(params.get("pageNumber"));
            int booksNumber = (int)Integer.parseInt(params.get("booksNumber"));
            int cursorPosition = (pageNumber - 1)*booksNumber +1;
            int userId = (int)Integer.parseInt(params.get("userId"));          

            System.out.println(userId);
            System.out.println(publishedBy);
            System.out.println(genreFilter);
            System.out.println(ordersFilterMap.get(orderFilter));
            
            cStmt = conn.prepareCall(ordersFilterMap.get(orderFilter));
            cStmt.setInt("genre_filter", genreFilter);
            cStmt.setInt("from_num", cursorPosition);
            cStmt.setInt("to_num", cursorPosition+booksNumber);
            cStmt.setInt("publ_by", publishedBy);
            cStmt.setInt("input_user_id", userId);
            cStmt.registerOutParameter("ds_ref_curs", OracleTypes.CURSOR);          

            cStmt.execute();
            rs = (ResultSet) cStmt.getObject("ds_ref_curs");      
            
            if(rs.next()){
              while(!rs.isAfterLast()) {
                    String isbn = rs.getString("isbn");                   
                    authors = ba.getBookInfo(isbn, conn);
                    genres = bg.getBookInfo(isbn, conn);
                    rating = bdr.getBookInfo(isbn, conn);                    
                    if (userId!=0) {  
                        curUserScore = cus.getCurrentUserScore(isbn, conn, userId);
                    }      
                    Book dashboardBook = new Book();
                    dashboardBook.setISBN(rs.getString("isbn"));
                    dashboardBook.setTitle(rs.getString("title"));  
                    dashboardBook.setImageURL(rs.getString("image"));
                    dashboardBook.setDescription(rs.getString("description"));
                    dashboardBook.setPageNumber(rs.getInt("page_number"));
                    dashboardBook.setAvgRating(rs.getFloat("avg_rating"));
                    dashboardBook.setSumRating(rs.getInt("sum_rating"));
                    dashboardBook.setAuthors(authors);
                    dashboardBook.setGenres(genres);
                    dashboardBook.setRating(rating);
                    dashboardBook.setCurrentUserScore(curUserScore);
  
                    dashboard.add(dashboardBook);
                    
                    rs.next();
               }  
            }
        } catch(SQLException ex) {
            throw ex;
        } finally { 
            try {
                if (rs != null) {
                    rs.close();
                }
            }
            finally {
                try {
                    if (cStmt != null) {
                        cStmt.close();
                    }
                }
                finally {
                    if (conn != null){
                        releaseConnection(conn);
                    }
                }
            }
        }     
        
        return dashboard;
    }
    
}
